package com.erpanderp.wam.ac.view.reimbursement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpenseDetails;
import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.service.reimbursement.MiscReimbursementDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class InprocessMiscllaneousExpensesBean implements Serializable{

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
	private TimeZone timeZone = TimeZone.getDefault();

	private MiscExpenseDetails miscExpenseDetails = new MiscExpenseDetails();
	private List<MiscExpenseDetails> miscExpensesDetailsList = new ArrayList<MiscExpenseDetails>();	
	
	private List<MiscExpense> miscExpenseInprocessList = new ArrayList<MiscExpense>();
	private List<MiscExpense> miscExpenseApprovedList = new ArrayList<MiscExpense>();
	private List<MiscExpense> miscExpenseRejectedList = new ArrayList<MiscExpense>();
	
	private MiscReimbursementDAO miscReimbursementDAO = new MiscReimbursementDAO();
	
	private int inprocessSNo=1;
	private int approvedSNo=1;
	private int rejectedSNo=1;

	public InprocessMiscllaneousExpensesBean() {
		
		miscExpenseInprocessList = miscReimbursementDAO.getMiscExpenseRequestsByApproverId(wamUser.getEmployee().getEmployeeId(), "IN PROCESS");
		for(MiscExpense miscExpense  : miscExpenseInprocessList){
			miscExpense.setsNo(inprocessSNo);
			miscExpense.setBriefPurposeDetails(miscExpense.getExpensePurpose()!= null && miscExpense.getExpensePurpose().length() > 20 ? miscExpense.getExpensePurpose().substring(0, 20) + "..." : miscExpense.getExpensePurpose() + "...");
			inprocessSNo++;
		}
		
		miscExpenseApprovedList = miscReimbursementDAO.getMiscExpenseRequestsByApproverId(wamUser.getEmployee().getEmployeeId(), "APPROVED");
		for(MiscExpense miscExpense  : miscExpenseApprovedList){
			miscExpense.setsNo(approvedSNo);
			miscExpense.setBriefPurposeDetails(miscExpense.getExpensePurpose()!= null && miscExpense.getExpensePurpose().length() > 20 ? miscExpense.getExpensePurpose().substring(0, 20) + "..." : miscExpense.getExpensePurpose() + "...");
			approvedSNo++;
		}
		
		miscExpenseRejectedList = miscReimbursementDAO.getMiscExpenseRequestsByApproverId(wamUser.getEmployee().getEmployeeId(), "REJECTED");
		for(MiscExpense miscExpense  : miscExpenseRejectedList){
			miscExpense.setsNo(rejectedSNo);
			miscExpense.setBriefPurposeDetails(miscExpense.getExpensePurpose()!= null && miscExpense.getExpensePurpose().length() > 20 ? (miscExpense.getExpensePurpose().substring(0, 20) + "...") : miscExpense.getExpensePurpose() + "...");
			rejectedSNo++;
		}
		
	}
	
	//Recommended by recommender 
	 public String miscExpenseApprovedAction() {
		 System.out.println("=====INSIDE travelExpenseRecommendAction() method=======");
		 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		  String miscCode = params.get("miscCode");
		  String empId = params.get("empId");
		  
		  
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("miscCode", miscCode);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("empId", empId);
		  
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/approvereimbursement.html?miscCode="+ miscCode+ "&empId="+empId);
		
		 
		  }catch (Exception e) {
			  e.printStackTrace();
		 }
		 return "approvereimbursement";
	 
		}




	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}


	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}


	public MiscExpenseDetails getMiscExpenseDetails() {
		return miscExpenseDetails;
	}


	public void setMiscExpenseDetails(MiscExpenseDetails miscExpenseDetails) {
		this.miscExpenseDetails = miscExpenseDetails;
	}


	public List<MiscExpenseDetails> getMiscExpensesDetailsList() {
		return miscExpensesDetailsList;
	}


	public void setMiscExpensesDetailsList(List<MiscExpenseDetails> miscExpensesDetailsList) {
		this.miscExpensesDetailsList = miscExpensesDetailsList;
	}

	public List<MiscExpense> getMiscExpenseInprocessList() {
		return miscExpenseInprocessList;
	}


	public void setMiscExpenseInprocessList(List<MiscExpense> miscExpenseInprocessList) {
		this.miscExpenseInprocessList = miscExpenseInprocessList;
	}


	public List<MiscExpense> getMiscExpenseApprovedList() {
		return miscExpenseApprovedList;
	}


	public void setMiscExpenseApprovedList(List<MiscExpense> miscExpenseApprovedList) {
		this.miscExpenseApprovedList = miscExpenseApprovedList;
	}


	public List<MiscExpense> getMiscExpenseRejectedList() {
		return miscExpenseRejectedList;
	}


	public void setMiscExpenseRejectedList(List<MiscExpense> miscExpenseRejectedList) {
		this.miscExpenseRejectedList = miscExpenseRejectedList;
	}


	public MiscReimbursementDAO getMiscReimbursementDAO() {
		return miscReimbursementDAO;
	}


	public void setMiscReimbursementDAO(MiscReimbursementDAO miscReimbursementDAO) {
		this.miscReimbursementDAO = miscReimbursementDAO;
	}


	public int getInprocessSNo() {
		return inprocessSNo;
	}


	public void setInprocessSNo(int inprocessSNo) {
		this.inprocessSNo = inprocessSNo;
	}
	public int getRejectedSNo() {
		return rejectedSNo;
	}


	public void setRejectedSNo(int rejectedSNo) {
		this.rejectedSNo = rejectedSNo;
	}


	public int getApprovedSNo() {
		return approvedSNo;
	}


	public void setApprovedSNo(int approvedSNo) {
		this.approvedSNo = approvedSNo;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

}
